package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wanglibao.woodtrade.woodtrade.R;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lugg on 2016/5/27.
 */
public class ViewpagerImgAdapter extends PagerAdapter {

    private List<View> homepageAdvertisementPicList;
    private List<String> imageUrls;
    int imageSize;

    public ViewpagerImgAdapter(Context context,List<String> imageUrls){
        this.imageUrls = imageUrls;
        homepageAdvertisementPicList = new ArrayList<View>();
        imageSize = imageUrls.size();
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        ImageOptions imageOptions = new ImageOptions.Builder().setLoadingDrawable(context.getResources().getDrawable(R.mipmap.ic_lose_p_loading))
                .setFailureDrawable(context.getResources().getDrawable(R.mipmap.ic_lose_p_fail)).
                        setCircular(true).build();
        for (int i = 0; i < imageSize; i++){
            ImageView img = new ImageView(context);
            img.setLayoutParams(layoutParams);
            Uri uri = Uri.parse(imageUrls.get(i));
            x.image().bind(img,imageUrls.get(i),imageOptions);

            homepageAdvertisementPicList.add(img);
        }
    }

    @Override
    public int getCount() {
        return imageSize;
    }

    // 是否是同一张图片
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    // 移除
    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        view.removeView(homepageAdvertisementPicList.get(position));
    }

    // 添加
    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        view.addView(homepageAdvertisementPicList.get(position));
        return homepageAdvertisementPicList.get(position);
    }
}
